package org.pet.mapper;

import org.apache.ibatis.annotations.*;
import org.pet.entity.PetScience;
import org.pet.util.Result;

import java.util.List;

@Mapper
public interface PetScienceMapper {

    //获取文章列表，分页展示
    @Select({"<script>",
            "select * from pet_science",
            "<where>",
            "<if test = 'petVariety != null'>pet_variety=#{petVariety} or pet_subVariety=#{petVariety}</if>",
            "<if test = 'state != null'>and state=#{state},</if>",
            "</where>",
            "</script>"})
    List<PetScience> list(String petVariety, String state);

    //获取文章详情
    @Select("select * from pet_science where id=#{id};")
    PetScience info(Integer id);

    //新增文章
    @Insert("insert into pet_science " +
            "(title, text, pet_variety, pet_subVariety, user_id, state, user_name, pic, score, create_time, modify_time, remove)" +
            "values " +
            "(#{title}, #{text}, #{petVariety}, #{petSubVariety}, #{userId}, #{state}, #{userName}, #{pic}, #{score}, now(), now(), #{remove})")
    int insert(PetScience petScience);
    //修改文章
    @Update({
            "<script>",
            "update pet_science",
            "<set>",
            "<if test = 'title != null'>title=#{title},</if>",
            "<if test = 'text != null'>text=#{text},</if>",
            "<if test = 'petVariety != null'>pet_variety=#{petVariety},</if>",
            "<if test = 'petSubVariety != null'>pet_subVariety=#{petSubVariety},</if>",
            "<if test = 'pic != null'>pic=#{pic},</if>",
            "modify_time=now()",
            "</set>",
            "where id=#{id}",
            "</script>"})
    int update(PetScience petScience);

    //删除文章
    @Delete("delete from pet_science where id=#{id}")
    int delete(Integer id);

    @Update("update pet_science set clicks = clicks + 1 where id = #{id}")
    int updateClick(Integer id);
}
